﻿/*
Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
For licensing, see LICENSE.html or http://ckeditor.com/license
*/

(function()
{
	/*
	 * 创建所见非所得的元素
	 * 创建代理元素 createFakeParserElement 
	 * 参见 http://docs.cksource.com/ckeditor_api/symbols/src/plugins_fakeobjects_plugin.js.html	 * 
	 * function( realElement, className, realElementType, isResizable )
	 */
	function createFakeElement( editor, realElement )
	{
		return editor.createFakeParserElement( realElement, 'cke_stab', 'stab', true );
	}

	CKEDITOR.plugins.add( 'spc',
	{
		/*
		 * 初始化插件
		 * editor.addCommand 
		 * Register the "stabs" command, which opens the "stabs" dialog.
		 */
		init : function( editor )
		{						
			var addSpcPlugin = function(mepath ,buttonName, commandName, dialogFile , defwidth , defheight)
			{
				/*
				 * 消除"未知对象"
				 */
				editor.lang.fakeobjects[commandName] = editor.lang.spc[commandName].typename;
				
				editor.addCommand( commandName, new CKEDITOR.dialogCommand( commandName ) );
				/*
				 * 初始化右键菜单group
				 */

				editor.ui.addButton( buttonName,
				{
					label : editor.lang.spc[commandName].title,
					command : commandName,
					icon: mepath + "icon/" + commandName +".png"
				});
				
				CKEDITOR.dialog.add( commandName, dialogFile );
				
				var dw = 80 ,dh= 80;
				if(defwidth) dw = defwidth ;
				if(defheight) dh = defheight ;
				/*
				 * 添加样式
				 */
				editor.addCss(
				'img.cke_' + commandName +
				'{' +
					'background-image: url(' + CKEDITOR.getUrl( mepath + 'images/'+ commandName +'.png' ) + ');' +
					'background-position: center center;' +
					'background-repeat: no-repeat;' +
					'border: 1px solid #a9a9a9;' +
					'width: '+ dw +'px;' +
					'height: '+ dh +'px;' +
				'}'
				);
				/*
				 * 双击事件
				 */
				editor.on( 'doubleclick', function( evt )
				{
					var element = evt.data.element;
					if ( element.is( 'img' ) && element.data( 'cke-real-element-type' ) == commandName )
						evt.data.dialog = commandName;
				});				
			};		
			
			//var mepath =this.path;
			var dialogPath = this.path + 'dialogs/';
			
			addSpcPlugin(this.path,'STab' , 'stab', dialogPath + 'stab.js' ,100 ,30);
			
			/*
			 * 增加菜单命令
			 */
			editor.addCommand( 'stab_manage',
				{
					exec : function( editor )
					{
						alert("tabs");
					}
			 	} );
			/*
			 * 增加右键菜单
			 */
			editor.addMenuGroup("stab");
			 
			if ( editor.addMenuItems )
			{
				
				//alert(editor.lang.spc['stab'].names);
				editor.addMenuItems(
					{
						stab :
						{
							label : editor.lang.spc["stab"].names ,
							icon:  this.path + "icon/stab.png",
							command : "stab",
							group : "stab"
						},
						stab_manage :
						{
							label : editor.lang.spc["stab"].manages ,
							//icon:   mepath + "icon/stab.png",
							command : "stab_manage",
							group : "stab"
						}
					});
			}

			
			if ( editor.contextMenu )
			{
				editor.contextMenu.addListener( function( element, selection )
					{
						if ( element && element.is( 'img' ) && !element.isReadOnly()
								&& element.data( 'cke-real-element-type' ) == "stab" )							
							return { stab : CKEDITOR.TRISTATE_OFF, stab_manage : CKEDITOR.TRISTATE_OFF };
					});
			}
			
		},

		afterInit : function( editor )
		{
			/*If defined, points to the data processor which 
			 * is responsible to translate and transform the editor data on input and output. 
			 * Generaly it will point to an instance of CKEDITOR.htmlDataProcessor, 
			 * which handles HTML data.
			 * The editor may also handle other data formats by using different data processors provided by specific plugins. 
			 * 数据处理器，处理编辑器和提交表单赋值过程的各个数据状态
			 * addRules ：增加数据过滤的规则
			 * 
			 */
			var dataProcessor = editor.dataProcessor,
				dataFilter = dataProcessor && dataProcessor.dataFilter;
			if ( dataFilter )
			{
				dataFilter.addRules(
				{
					elements :
					{
						'img' : function( element )
						{
							if ( element.attributes.type == 'spc_stab' )
								return editor.createFakeParserElement( element, 'cke_stab', 'stab', true );
						}
					}
				});
			}
		},

		requires : ['fakeobjects' ],
		
		lang : ['en','zh-cn']
	});
})();
